package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.common.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;


    @RequestMapping("")
    public String index(Model model) {

        //upCds : 飙升排行榜(评论数+阅读数)前10
        model.addAttribute("upCds", strategyStatsCacheService.listRankTop10(RedisKey.STRATEGY_STATS_UP_SORT));

        //hotCds : 热门排行榜(点赞数+收藏数)前10
        model.addAttribute("hotCds", strategyStatsCacheService.listRankTop10(RedisKey.STRATEGY_STATS_HOT_SORT));

        //commends : 推荐的攻略(包装标题)前5
        model.addAttribute("commends", strategyCommendService.listTop5());

        //构建高级查询条件
        //unabroads :国内的省份数据
        model.addAttribute("unabroads", strategySearchService.listCondition(SearchQueryObject.CONDITION_UNABROAD));
        //abroads :国外的国家数据
        model.addAttribute("abroads", strategySearchService.listCondition(SearchQueryObject.CONDITION_ABROAD));
        //themes :主题数据
        model.addAttribute("themes", strategySearchService.listCondition(SearchQueryObject.CONDITION_THEME));
        //themeCds : 推荐的主题
        model.addAttribute("themeCds", strategySearchService.themeCommend());

        return "strategy/index";
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        //吐司
        List<Destination> toasts = destinationService.listToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);
        //目的地
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));

        List<StrategyTag> tags = strategyTagService.listByDestId(qo.getDestId());
        //tags :该目的地该文章下的标签
        model.addAttribute("tags", tags);

        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));

        return "strategy/list";
    }

    @RequestMapping("/detail")
    public String detail(Long id, @LoginUser UserInfo user, Model model) {

        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail", detail);
        //阅读数 +1 操作
        strategyStatsCacheService.incr(id, 1, IStrategyStatsCacheService.VIEWNUM_TYPE);
        //攻略vo:当前文章的数据统计对象
        StrategyStats vo = strategyStatsCacheService.getStrategyStats(id);
        model.addAttribute("vo", vo);

        //isFavor:回显当前收藏结果
        if (user != null) {
            boolean favor = strategyStatsCacheService.isFavor(id, user.getId());
            model.addAttribute("isFavor", favor);
        }
        //飙升排行阅读加分操作
        strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_UP_SORT, 1, id);
        return "strategy/detail";
    }

    @RequireLogin
    @ResponseBody
    @RequestMapping("/commentAdd")
    public Object commentAdd(StrategyComment entity, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        //填充数据
        entity.setUserId(user.getId());
        entity.setUsername(user.getNickname());
        entity.setHeadUrl(user.getHeadImgUrl());
        entity.setLevel(user.getLevel());
        //保存操作
        strategyCommentService.save(entity);
        //评论加分
        strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_UP_SORT, 1, entity.getDetailId());
        //返回评论数据
        StrategyStats stats = strategyStatsCacheService.getStrategyStats(entity.getDetailId());
        return result.addData(stats.getReplynum());//回显评论

    }

    @RequestMapping("/comment")//查询评论内容
    public Object comment(StrategyCommentQuery qo, Model model) {
        //page Spring Data封装的分页对象
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page", page);

        return "strategy/commentTpl";
    }

    @RequireLogin
    @ResponseBody
    @RequestMapping("/commentThumbUp")//文章评论点赞
    public Object commentThumbUp(String toId, Long fromId) {
        AjaxResult result = new AjaxResult();
        try {
            strategyCommentService.commentThumbUp(toId, fromId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("点赞失败");
        }
        return result;
    }

    @RequireLogin
    @ResponseBody
    @RequestMapping("/favor")//收藏
    public Object favor(Long strategyId, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        //设置收藏结果
        boolean success = strategyStatsCacheService.favor(strategyId, user.getId());
        result.setSuccess(success);

        //返回收藏数
        StrategyStats stats = strategyStatsCacheService.getStrategyStats(strategyId);

        int num = success ? 1 : -1;//收藏+1 : 取消收藏-1
        strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_HOT_SORT, num, strategyId);

        return result.addData(stats.getFavornum());//临时同步收藏的统计值
    }

    @RequireLogin
    @ResponseBody
    @RequestMapping("/strategyThumbup")//顶(点赞)
    public Object strategyThumbup(Long strategyId, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        boolean success = strategyStatsCacheService.strategyThumbup(strategyId, user.getId());
        result.setSuccess(success);

        //返回点赞(顶)数
        StrategyStats stats = strategyStatsCacheService.getStrategyStats(strategyId);
        strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_HOT_SORT, 1, strategyId);
        return result.addData(stats.getThumbsupnum());
    }

    @RequestMapping("/searchPage")
    public Object searchPage(SearchQueryObject qo, Model model) {
        //page
        model.addAttribute("page", strategySearchService.query(qo));
        return "strategy/searchPageTpl";
    }
}
